package com.ostrich.cloud.ostrichUserService.app.controller;



import com.ostrich.cloud.common.result.PageResult;
import com.ostrich.cloud.common.result.Result;
import com.ostrich.cloud.ostrichUserService.app.config.JwtHelper;
import com.ostrich.cloud.ostrichUserService.client.AdminMangerClient;
import com.ostrich.cloud.ostrichUserService.infra.model.UserParent;
import com.ostrich.cloud.ostrichUserService.service.AdminService;
import com.ostrich.ostrichUserService.dto.AdminDto;
import com.ostrich.ostrichUserService.param.CusPasswordParam;
import com.ostrich.ostrichUserService.param.admin.RegAdminParam;
import com.ostrich.ostrichUserService.param.bussiness.LoginBusParam;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;

/**
 * <p>
 * 管理员表 前端控制器
 * </p>
 *
 * @author Lucas
 * @since 2022-10-26
 */
@RestController
public class AdminController implements AdminMangerClient {

    @Resource
    private AdminService adminService;

    @Resource
     private JwtHelper jwtHelper;
    @Resource
    private CustomerController customerController;
    @Override
    public Result addAdmin(RegAdminParam regAdminParam) {

        adminService.addAdmin(regAdminParam);
        return Result.okm("添加成功");
    }

    @Override
    public Result adminLogin(LoginBusParam loginBusParam) {
        UserParent userParent = adminService.adminLogin(loginBusParam);
        Date date = DateUtils.addDays(new Date(), 1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("operatorId",userParent.getParentId());
        map.put("operatorName","admin");
        String jwt = jwtHelper.createJWT(date, map);
        //  第一步：比对前端提交的账号名称、密码
        return Result.ok(jwt);
    }

    @Override
    public Result updateAdminPassword(CusPasswordParam cusPasswordParam) {
       return customerController.cusPasswordUp(cusPasswordParam);
    }

    @Override
    public Result getAllAdmins(Integer current, Integer size, String status) {
        PageResult<AdminDto> allAdmins = adminService.getAllAdmins(current, size, status);

        return Result.ok(allAdmins);
    }

    @Override
    public Result updateAdmin(Integer parentId, String status) {
        adminService.updateAdmin(parentId,status);
        return Result.okm("修改成功");
    }
}

